﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.OS
{
	public partial class Linux
	{ /// <summary>
	  /// 获取无线网络列表
	  /// </summary>
		public static void GetSSIDList(string wlan)
		{
			string cmd = $"sudo iw dev {wlan} scan";
		}

		public static void OpenWLAN(string ifname)
		{
			string cmd = $"sudo ip link set {ifname} up";
		}

		public static void CloseWLAN(string ifname)
		{
			string cmd = $"sudo ip link set {ifname} down";

		}

		/// <summary>
		/// 连接到指定的接入点   wpa_supplicant
		/// </summary>
		/// <param name="wlan">devname</param>
		/// <param name="SSID">接入点名称</param>
		/// <param name="freq">频道</param>
		/// <param name="psw"></param>
		public static void ConnectWLAN(string wlan, string SSID, int freq, string psw)
		{//-w:等待连接完成。

			string cmd = $"iw dev -w wlan0 connect {SSID} key 0:{psw}";
		}

		public static void DisConnectWLAN(string wlan)
		{
			string cmd = $"iw dev wlan0 disconnect";
		}

		public static void DhcpEnable(string ifname)
		{
			string cmd = $"sudo dhclient {ifname}";
		}
		public static void DhcpUnEnable(string ifname)
		{
			string cmd = $"sudo dhclient -r {ifname}";
		}

		public static string GetOSType()
		{
			return Linux.ReadTextFile("/proc/sys/kernel/ostype");
		}

		public static string GetOSVersion()
		{
			return Linux.ReadTextFile("/proc/sys/kernel/version");
		}
		public static string GetOSRelease()
		{
			return Linux.ReadTextFile("/proc/sys/kernel/osrelease");
		}
		/// <summary>
		/// 获取网卡列表
		/// </summary>
		/// <returns></returns>
		public static inter_face[] GetNetCardList()
		{
			var txt = Linux.shell("ifconfig");
			//收集ifconfig反馈的数据
			//using (var fs = new FileStream("/home/orangepi/a.txt", FileMode.Create))
			//{
			//    using (var sw = new StreamWriter(fs, Encoding.UTF8))
			//    {
			//        sw.Write(txt);
			//    }
			//}
			List<inter_face> array = new List<inter_face>();

			var es = txt.Split(new string[] { "/n/n" }, StringSplitOptions.RemoveEmptyEntries);
			foreach (var e in es)
			{
				inter_face i_f = new inter_face();
				var lines = e.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
				var firstline = true;
				foreach (var line in lines)
				{
					var fields = line.Split(new string[] { "  " }, StringSplitOptions.RemoveEmptyEntries);
					if (firstline)
					{
						i_f.ifname = fields[0];
						firstline = false;
					}
					foreach (var field in fields)
					{
						if (field.StartsWith("Link encap:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.link_encap = field.Substring(11);
						}
						else if (field.StartsWith("HWaddr", StringComparison.OrdinalIgnoreCase))
						{
							i_f.HWaddr = field.Substring(7);
						}
						else if (field.StartsWith("inet addr:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.inet_addr = field.Substring(10);
						}
						else if (field.StartsWith("Bcast:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.bcast = field.Substring(6);
						}
						else if (field.StartsWith("Mask:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.mask = field.Substring(5);
						}
						else if (field.StartsWith("inet6 addr:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.inet6_addr = field.Substring(12);
						}
						else if (field.StartsWith("Scope:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.scope = field.Substring(6);
						}
						else if (field.StartsWith("MTU:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.MTU = field.Substring(4);
						}
						else if (field.StartsWith("Metric:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.metric = field.Substring(7);
						}
						else if (field.StartsWith("RX", StringComparison.OrdinalIgnoreCase))
						{
							i_f.RX = field.Substring(3);
						}
						else if (field.StartsWith("TX", StringComparison.OrdinalIgnoreCase))
						{
							i_f.TX = field.Substring(3);
						}
						else if (field.StartsWith("collisions:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.collisions = field.Substring(11);
						}
						else if (field.StartsWith("txqueuelen:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.txqueuelen = field.Substring(11);
						}
						else if (field.StartsWith("RX bytes:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.RX_bytes = field.Substring(9);
						}
						else if (field.StartsWith("TX bytes:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.TX_bytes = field.Substring(9);
						}
						else if (field.StartsWith("Interrupt:", StringComparison.OrdinalIgnoreCase))
						{
							i_f.interrupt = field.Substring(10);
						}
						else
						{
							i_f.UP = field.Contains("UP");
							i_f.RUNNING = field.Contains("RUNNING");
							i_f.BROADCAST = field.Contains("BROADCAST");
							i_f.MULTICAST = field.Contains("MULTICAST");
							i_f.LOOPBACK = field.Contains("LOOPBACK");

						}
					}
				}
				array.Add(i_f);
			}
			return array.ToArray();
		}
		/// <summary>
		/// 获取块设备的信息
		/// </summary>
		/// <returns></returns>
		public static disk[] blkid()
		{
			var txt = Linux.shell("blkid");
			var lines = txt.Split('\n');
			List<disk> l = new List<disk>();
			foreach (var line in lines)
			{
				disk d = new disk();
				var items = line.Split(' ');
				foreach (var item in items)
				{
					var kv = item.Split('=');
					if (kv.Length == 1)
					{
						d.PATH = kv[0];
					}
					else if (kv.Length == 2)
					{
						var key = kv[0];
						var val = kv[1];
						switch (key)
						{
							case "UUID":
								d.UUID = val.Trim('"');
								break;
							case "TYPE":
								d.TYPE = val.Trim('"');
								break;
							case "PARTUUID":
								d.PARTUUID = val.Trim('"');
								break;
							case "LABEL":
								d.LABEL = val.Trim('"');
								break;
							default:
								break;
						}
					}
				}
				l.Add(d);
			}
			return l.ToArray();
		}

	}
}
